package com.oneguy.client;

import com.oneguy.client.log.Log;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;

/**
 * Created by ZuoShu on 17/9/14.
 */
public class Client implements TcpConnectionListener {
    private static final String TAG = Client.class.getSimpleName();
    private static String proFileName = "/clientConfig.properties";
    public String serverIp;
    public int serverPort;
    private Scanner scanner;
    private boolean running;
    private TcpConnection tcpConnection;

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    public Client() {
        scanner = new Scanner(System.in);
        running = true;
    }

    private void readConfig() {
        InputStream in = null;
        try {
            Properties properties = new Properties();
            in = ClassLoader.class.getResourceAsStream(proFileName);
            properties.load(in);
            serverIp = properties.getProperty("server_ip");
            serverPort = Integer.parseInt(properties.getProperty("server_port"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    private void start() {
        readConfig();
        printHelp();
        connect();
        while (running) {
            String command = readCommand();
            processCommand(command);
        }
    }

    private void stop() {
        if (tcpConnection != null) {
            tcpConnection.disconnect();
            tcpConnection = null;
        }
        running = false;
        System.exit(1);
    }


    private void processCommand(String command) {
        if (command.startsWith("stop")) {
            stop();
        } else if (command.startsWith("connect")) {
            connect();
        } else if (command.startsWith("sendGapHex")) {
            sendGapHex(command.substring(command.indexOf(" ")));
        } else if (command.startsWith("sendHex")) {
            String[] args = command.split(" ");
            sendHexData(args[1]);
        } else if (command.startsWith("send")) {
            String[] args = command.split(" ");
            sendData(args[1]);
        } else {
            printHelp();
        }
    }

    private void printHelp() {
        System.out.println("support command:");
        System.out.println("    send 123456");
        System.out.println("    sendHex 0a0b0c");
        System.out.println("    sendGapHex 0a 0b 0c");
        System.out.println("    stop");
    }

    private void sendData(String str) {
        sendData(str.getBytes());
    }

    private void sendData(byte[] data) {
        if (tcpConnection == null) {
            Log.d(TAG, "socket not connect!");
            return;
        }

        try {
            tcpConnection.sendMessage(data);
        } catch (IOException e) {
            Log.e(TAG, "send message fail:" + e.getMessage());
            e.printStackTrace();
        }
    }

    private void sendHexData(String str) {
        sendData(Utils.parseHexBinary(str));
    }

    private void sendGapHex(String str) {
        str = str.replace(" ", "");
        sendHexData(str);
    }

    private String readCommand() {
        return scanner.nextLine();
    }

    private void connect() {
        tcpConnection = new TcpConnection();
        try {
            tcpConnection.setListener(this);
            tcpConnection.connect(serverIp, serverPort);
        } catch (ConnectionException e) {
            e.printStackTrace();
        }
    }

    public void onConnect(TcpConnection connection) {
        Log.d(TAG, "onConnect:" + (connection == null ? null : connection.getSocket().getInetAddress()));
    }

    public void onDisconnect(TcpConnection connection) {
        Log.d(TAG, "onDisconnect:" + (connection == null ? null : connection.getSocket().getInetAddress()));
    }

    public void onException(TcpConnection connection, Exception e) {
        Log.d(TAG, "onException:" + e.toString());
    }

    public void onReceive(TcpConnection connection, byte[] data) {
        Log.d(TAG, "onReceive:" + Utils.printHexBinary(data));
    }

    public void onSend(TcpConnection connection, byte[] data) {
        Log.d(TAG, "onSend:" + Utils.printHexBinary(data));
    }
}
